<?php

class User extends CI_Model
{

    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
        $this->load->database();
    }

    /**
     * 根据指定条件获取user信息
     *
     * @param $data
     * @return mixed
     */
    function get_user($data)
    {
        $query = $this->db->get_where('thinkcms_user', $data);
        return $query->row_array();
    }

    /**
     * 获取中奖用户
     *
     * @param $limit
     * @param $start
     * @return mixed
     */
    function get_prize_user($limit, $start)
    {
        $sql = 'select * from prize_user  ORDER BY id DESC limit '.$start.','.$limit;
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    /**
     * 获取所有中奖信息
     *
     * @return mixed
     */
    function  get_all_prize_user()
    {
        return $this->db->count_all_results('prize_user');
    }

    /**
     * 更新user
     *
     * @param $data
     */
    function update_user($data)
    {
        $this->db->where('username', 'admin');
        $this->db->update('thinkcms_user', $data);
    }



    /**
     * 获取日期
     *
     * @param $limit
     * @param $start
     * @return mixed
     */
    function get_prize_use_data()
    {
        $sql = 'select count(*) as count, SUBSTRING(create_time,1,10) as time  from prize_user group by SUBSTRING(create_time,1,10) ORDER BY id DESC ';
        $query = $this->db->query($sql);
        return $query->result_array();
    }


    function get_prize_use_data_detail($time)
    {
        $sql =  "SELECT
                        mobile,
                        create_time,
                        ( CASE prize_type when 0 then 1   WHEN 1 then 8 when 2 then 16 else 24 end) as m
                    FROM
                        prize_user
                    WHERE
                        DATE_FORMAT(
                            `create_time`,
                            '%Y-%m-%d %H:%i:%s'
                        ) >= '{$time} 00:00:00'
                    AND DATE_FORMAT(
                        `create_time`,
                        '%Y-%m-%d %H:%i:%s'
                    ) <= '{$time} 23:59:59'";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

}